package com.fangcang.concurrency.example.threadPool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ThreadPoolExample5 extends ThreadPoolExecutor {

	@Override
	protected void beforeExecute(Thread t, Runnable r) {
		super.beforeExecute(t, r);
	}

	@Override
	protected void afterExecute(Runnable r, Throwable t) {
		super.afterExecute(r, t);
		//总的任务数，当前工作的任务+已完成的任务数
		log.info("总任务数{}", this.getTaskCount());
		//当前工作任务队列中的线程数
		log.info("当前激活任务数{}", this.getActiveCount());
		//线程池最大任务数
		log.info("线程池最大任务数{}", this.getLargestPoolSize());
		
		log.info("当前队列大小{}", this.getQueue().size());
	}

	public ThreadPoolExample5(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
	}

	public static void main(String[] args) {

		ThreadPoolExample5 pool = new ThreadPoolExample5(2, 10, 1000, TimeUnit.SECONDS, new ArrayBlockingQueue<>(20));
		
		for(int i=0;i <50; i++) {
			pool.execute(()->{
				System.out.println(Thread.currentThread().getName());
			});
		}
		
		pool.shutdown();
	}
}
